<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/29 15:11
 * Remark:
 */

namespace App\Func;
use Exception;
use Illuminate\Support\Facades\Http;

class RequestFunc
{
    /**
     * 获取请求头中的参数
     * @param $param
     * @return array|string|null
     */
    public static function getHeaderParams($param): array|string|null
    {
        return request()->header($param);
    }

    public static function getIP(){
        $ip = NULL;
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $arr = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $pos = array_search('unknown',$arr);
            if(false !== $pos) unset($arr[$pos]);
            $ip  = trim($arr[0]);
        }elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        }elseif (isset($_SERVER['REMOTE_ADDR'])) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        $long = sprintf("%u", ip2long($ip));
        $ip   = $long ? array($ip, $long) : array('0.0.0.0', 0);
        return $ip[0];
    }

    public function xmlGet($url,$xmlData){
        // 初始化 cURL
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, [
            'Content-Type: application/xml',
            'Accept: application/xml'
        ]);
        $response = curl_exec($ch);
        $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
        if (curl_errno($ch)) {
            echo 'cURL 错误: ' . curl_error($ch);
        }
        curl_close($ch);
        echo $response;
    }

}